package com.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srb.common.exception.Assert;
import com.srb.common.result.ResponseEnum;
import com.srb.core.enums.UserBindEnum;
import com.srb.core.hfb.FormHelper;
import com.srb.core.hfb.HfbConst;
import com.srb.core.hfb.RequestHelper;
import com.srb.core.mapper.UserBindMapper;
import com.srb.core.mapper.UserInfoMapper;
import com.srb.core.pojo.entity.UserBind;
import com.srb.core.pojo.entity.UserInfo;
import com.srb.core.pojo.vo.UserBindVo;
import com.srb.core.service.UserBindService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-08-08
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVo userBindVo, Long userId) {
        QueryWrapper<UserBind> queryWrapper = new QueryWrapper<>();
        //1、不同的user_id,相同的身份证号,如果存在,则不允许
        queryWrapper.eq("id_card",userBindVo.getIdCard());
        queryWrapper.ne("user_id",userId);
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        //2、判断用户是否曾经绑定过表单
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        userBind = baseMapper.selectOne(queryWrapper);
        //2.1、创建用户绑定记录
        if (null == userBind) {
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVo, userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        }else {
            //2.2、相同的user_id不同的身份证号,如果存在,则更新
            BeanUtils.copyProperties(userBindVo,userBind);
            baseMapper.updateById(userBind);
        }
        Map<String, Object> paramMap = getParamMap(userBindVo, userId);
        //3、生成动态表单字符串
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL,paramMap);
        return formStr;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notify(Map<String, Object> paramMap) {
        String bindCode = paramMap.get("bindCode").toString();
        String agentUserId = paramMap.get("agentUserId").toString();
        //更新用户绑定表
        QueryWrapper<UserBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",agentUserId);
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);
        //更新用户表
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);
        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfoMapper.updateById(userInfo);
    }

    private Map<String, Object> getParamMap(UserBindVo userBindVo, Long userId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId",HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard", userBindVo.getIdCard());
        paramMap.put("personalName", userBindVo.getName());
        paramMap.put("bankNo", userBindVo.getBankNo());
        paramMap.put("bankType", userBindVo.getBankType());
        paramMap.put("mobile", userBindVo.getMobile());
        paramMap.put("returnUrl",HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl",HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        return paramMap;
    }
}
